﻿Public Class DialogRole
    Public Sub RefreshTableRole()
        Dim dt As New DataTable
        'Dim dsrole As New DataSet
        If Utils.executeSP("proc_role", New Object() {"select", TextKodeRole.Text, TextRole.Text}, dt) Then
            If dt IsNot Nothing Then
                DGVRole.DataSource = dt
            End If
        End If
    End Sub
    Private Sub DialogRole_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RefreshTableRole()
    End Sub

    Private Sub DGVRole_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVRole.CellClick
        If e.RowIndex = -1 Then Exit Sub
    End Sub
    Private Function ValidasiRole() As String
        If TextKodeRole.Text = "" Then
            Return "Kode masih kosong."
        End If
        Return "Success"
    End Function
    Private Sub MbGlassButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddRole.Click
        If btnAddRole.Text = "Add" Then
            btnEditRole.Enabled = False
            btnDelRole.Enabled = False
            DGVRole.Enabled = False
            PanelRole.Enabled = True
            TextKodeRole.Text = ""
            TextRole.Text = ""
            btnAddRole.Text = "Save"
            TextKodeRole.Focus()
        Else
            Dim res As String = ValidasiRole()
            If res = "Success" Then
                Dim dt As New DataTable
                If Utils.executeSP("proc_role", New Object() {"add", TextKodeRole.Text, TextRole.Text}, dt) Then
                    If dt IsNot Nothing Then
                        MessageBox.Show(dt.Rows(0)("pesan").ToString)
                        'Utils.exec_SP("proc_zloguser", New Object() {"add", "proc_role|add", TextKodeRole.Text, Session.username})
                    End If
                End If
            Else
                MessageBox.Show(res, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            End If
            RefreshTableRole()
            btnAddRole.Text = "Add"
            btnEditRole.Enabled = True
            btnDelRole.Enabled = True
            DGVRole.Enabled = True
            PanelRole.Enabled = False
            'RefreshComPetugas()
        End If
    End Sub

    Private Sub MbGlassButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MbGlassButton3.Click
        Me.Close()
    End Sub

    Private Sub btnEditRole_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditRole.Click
        If btnEditRole.Text = "Edit" Then
            btnDelRole.Enabled = False
            btnAddRole.Enabled = False
            'TextKode.Enabled = True
            PanelRole.Enabled = True
            btnEditRole.Text = "Update"
            TextKodeRole.Focus()
        Else
            btnEditRole.Text = "Edit"
            btnDelRole.Enabled = True
            btnAddRole.Enabled = True
            PanelRole.Enabled = False
            Dim dt As New DataTable
            If Utils.executeSP("proc_role", New Object() {"edit", TextKodeRole.Text, TextRole.Text}, dt) Then
                If dt IsNot Nothing Then
                    MessageBox.Show(dt.Rows(0)("pesan").ToString)
                    'Utils.exec_SP("proc_zloguser", New Object() {"add", "proc_role|edit", TextKodeRole.Text, Session.username})
                End If
            End If
            RefreshTableRole()
        End If
    End Sub

    Private Sub btnDelRole_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelRole.Click
        If TextKodeRole.Text = "" Then
            MessageBox.Show("Pilih Data Yang ingin dihapus", "Pesan Error", MessageBoxButtons.OK)
        Else
            Dim retval As Integer = MessageBox.Show("Apakah anda yakin akan menghapus " & TextKodeRole.Text & " ?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
            If retval = DialogResult.Yes Then
                Dim dt As New DataTable
                If Utils.executeSP("proc_role", New Object() {"delete", TextKodeRole.Text, TextRole.Text}, dt) Then
                    If dt IsNot Nothing Then
                        MessageBox.Show(dt.Rows(0)("pesan").ToString)
                        'Utils.exec_SP("proc_zloguser", New Object() {"add", "proc_role|delete", TextKodeRole.Text, Session.username})
                    End If
                End If
            End If
            RefreshTableRole()
        End If
    End Sub

    Private Sub DGVRole_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DGVRole.SelectionChanged
        With DGVRole
            Try
                TextKodeRole.Text = .CurrentRow.Cells(0).Value.ToString
                TextRole.Text = .CurrentRow.Cells(1).Value.ToString
            Catch ex As Exception

            End Try

        End With
    End Sub
End Class